package br.com.vti.sisp_sicrim.antigo.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;

import br.com.vti.sisp_sicrim.antigo.dao.util.HibernateUtil;

public class SicOperacaoDAO {

	private int codigoRetorno = 0; // *ERROR
	private String msgRetorno = ""; // *ERROR
	public long qtdRegistros = 0; // *counter
	public long nroRegistro = 0; // *counter
	public long nroRegistroModdis = 0; // *counter
	private String[] listacol = { "", "Operacao", "Programa", "Descricao", "Funcao", "Modulo", "DataAtu", "MatriculaFunc" };
	private ResultSet query = null;
	private ResultSet queryModdis = null;

	public String insert(String pOperacao, String pPrograma, String pDescricao, String pFuncao, Integer pModulo, Integer pDataAtu, Integer pMatriculaFunc, Connection con) {

		String sql = "{ ? = call sp_grava_Sic_Operacao ( ?,?,?,?,?,?,? ) }";
		UUID pId;
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {

			CallableStatement stp = con.prepareCall(sql);
			stp.registerOutParameter(1, Types.OTHER);
			stp.setString(2, pOperacao); // (getColuna("Operacao")
			stp.setString(3, pPrograma); // (getColuna("Programa")
			stp.setString(4, pDescricao); // (getColuna("Descricao")
			stp.setString(5, pFuncao); // (getColuna("Funcao")
			stp.setInt(6, pModulo); // (getColuna("Modulo")
			stp.setInt(7, pDataAtu); // (getColuna("DataAtu")
			stp.setInt(8, pMatriculaFunc); // (getColuna("MatriculaFunc")
			// execute SP
			stp.execute();
			pId = (UUID) stp.getObject(1);
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
		return String.valueOf(pId);
	}

	public void insertModdis(String pId, int pSeq, Integer pModdis, Connection con) {
		String sql = "{ call sp_grava_Sic_OperacaoModdis ( ?, ?, ? ) }";
		try {

			CallableStatement stp = con.prepareCall(sql);
			stp.setObject(1, UUID.fromString(pId)); // getColuna("Id")
			stp.setInt(2, pSeq); // getColuna("Seq")
			stp.setInt(3, pModdis); // getColuna("Moddis")
			stp.execute();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

	}

	public void update(String pId, String pOperacao, String pPrograma, String pDescricao, String pFuncao, Integer pModulo, Integer pDataAtu, Integer pMatriculaFunc, Connection con) {

		String sql = "{ call sp_atualiza_Sic_Operacao ( ?,?,?,?,?,?,?,?,? ) }";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {

			CallableStatement stp = con.prepareCall(sql);
			stp.setObject(1, UUID.fromString(pId)); // getColuna("Id")
			stp.setString(2, pOperacao); // getColuna("Operacao")
			stp.setString(3, pPrograma); // getColuna("Programa")
			stp.setString(4, pDescricao); // getColuna("Descricao")
			stp.setString(5, pFuncao); // getColuna("Funcao")
			stp.setInt(6, pModulo); // getColuna("Modulo")
			stp.setInt(7, pDataAtu); // getColuna("DataAtu")
			stp.setInt(8, pMatriculaFunc); // getColuna("MatriculaFunc")
			stp.setInt(9, 0); //
			// execute SP
			stp.execute();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}
	}

	public void updateModdis(String pId, int pSeq, Integer pModdis, Connection con) {
		String sql = "{ call sp_atualiza_Sic_OperacaoModdis ( ?, ?, ? ) }";
		try {

			CallableStatement stp = con.prepareCall(sql);
			stp.setObject(1, UUID.fromString(pId)); // getColuna("Id")
			stp.setInt(2, pSeq); // getColuna("Seq")
			stp.setInt(3, pModdis); // getColuna("Moddis")
			stp.executeUpdate();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

	}

	public void delete(String pId) {

		String sql = "{ call sp_exclui_Sic_Operacao ( ? ) }";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {

			CallableStatement stp = HibernateUtil.getConnection().prepareCall(sql);
			stp.setObject(1, UUID.fromString(pId)); // getColuna("Id")
			// execute SP
			stp.execute();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}
	}

	public void deleteModdis(String pId, int pSeq, Integer pModdis) {
		String sql = "{ call sp_exclui_Sic_OperacaoModdis ( ?, ?, ? ) }";
		try {

			CallableStatement stp = HibernateUtil.getConnection().prepareCall(sql);
			stp.setObject(1, UUID.fromString(pId)); // getColuna("Id")
			stp.setInt(2, pSeq); // getColuna("Seq")
			stp.setInt(3, pModdis); // getColuna("Moddis")
			stp.execute();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

	}

	public ResultSet selectMdfuop(String pFuncao, String pOperacao, Integer pModulo) {

		String sql = "select * from sp_consMdfuop_Sic_Operacao (";
		sql = sql + "pFuncao := '" + pFuncao + "',";
		sql = sql + "pOperacao := '" + pOperacao + "',";
		sql = sql + "pModulo := '" + pModulo + "' ";
		sql = sql + ")";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet readMdfuop() {

		String sql = "select * from sp_leMdfuop_Sic_Operacao ()";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet readMdfuopge(String pFuncao, String pOperacao, Integer pModulo) {

		String sql = "select * from sp_leMdfuopMaiorig_Sic_Operacao (";
		sql = sql + "pFuncao := '" + pFuncao + "',";
		sql = sql + "pOperacao := '" + pOperacao + "',";
		sql = sql + "pModulo := '" + pModulo + "' ";
		sql = sql + ")";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet readOperacao() {

		String sql = "select * from sp_leOperacao_Sic_Operacao ()";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet findOperacao(String pOperacao) {

		String sql = "select * from sp_pesqOperacao_Sic_Operacao (";
		sql = sql + "pOperacao := '" + pOperacao + "' ";
		sql = sql + ")";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet pesquisaProximo() {
		// query
		try {
			if (query == null)
				return null;
			if (query.getRow() == 0) {
				query = null;
				return null;
			}
			query.next();
			if (query.getRow() == 0) {
				query = null;
				return null;
			}
			if (query != null) {
				nroRegistro++;
			}
			return query;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
	}

	public boolean temProximo() {
		try {
			if (query == null)
				return false;
			if (query.getRow() == 0) {
				return false;
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return false;
		}
		return (nroRegistro < qtdRegistros);
	}

	public void fecha() {
		if (query == null)
			return;
		try {
			query.close();
			query = null;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			query = null;
		}
	}

	public ResultSet pesquisaModdis(String id) {

		String sql = "select * from sp_pesquisa_Moddis_Sic_Operacao ( pId := '" + id + "' )";
		try {
			queryModdis = HibernateUtil.executarQuery(sql);
			queryModdis.first();
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return queryModdis;
	}

	public ResultSet proximoModdis() {
		try {
			if (queryModdis == null)
				return null;
			if (queryModdis.getRow() == 0) {
				queryModdis = null;
				return null;
			}
			queryModdis.next();
			if (queryModdis.getRow() == 0) {
				queryModdis = null;
				return null;
			}
			if (queryModdis != null) {
				nroRegistroModdis++;
			}
			return queryModdis;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
	}

	public int getColuna(String coluna) {
		for (int i = 0; i < listacol.length; i++) {
			if (listacol[i].equals(coluna))
				return i;
		}
		return -1;
	}

}
